package top.aicey.ai.sparring.handler;

import com.alibaba.fastjson2.JSON;
import top.aicey.ai.sparring.dto.enums.AliAIAgentCallbackEventEnum;
import top.aicey.ai.sparring.dto.req.AliAIAgentCallback;
import top.aicey.ai.sparring.dto.req.AliAgentCallbackUserData;
import top.aicey.ai.sparring.handler.dto.AgentStopEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class CallbackEventDispatcher {

    @Autowired
    private ApplicationContext context;


    public void publisher(AliAIAgentCallback callback){
        String userData = callback.getUserData();
        AliAgentCallbackUserData aliAgentCallbackUserData = JSON.parseObject(userData, AliAgentCallbackUserData.class);


        if (AliAIAgentCallbackEventEnum.agent_stop.equals(callback.getEvent())){
            AgentStopEvent
                event = new AgentStopEvent(aliAgentCallbackUserData.getInstanceId(),callback.getAiAgentId(),callback.getInstanceId());
            context.publishEvent(event);
        }
    }

}
